//
//  HJStatusPictureView.swift
//  WeiBo
//
//  Created by 韩俊 on 15/12/13.
//  Copyright © 2015年 HanJun. All rights reserved.
//

import UIKit
import SnapKit
// 重用标识符
private let HJStatusPictureViewItemId = "HMStatusPictureViewItemId"

private let itemMargin: CGFloat = 5

private let itemWH = (SCREENW - itemMargin * 2 - HJStatusCellMargin * 2) / 3

class HJStatusPictureView: UICollectionView {
    
    var sizeConstraint: Constraint?
    
    var pic_urls: [HJStatusPictureInfo]?{
        
        didSet{
            
            countlabel.text = "\(pic_urls?.count ?? 0)"
            
            let size = calcSize(pic_urls?.count ?? 0)
            
            snp_updateConstraints { (make) -> Void in
                 make.size.equalTo(size)
            }
            
            reloadData()
        }
    }
    
    private func calcSize(count:Int) -> CGSize {
        let itemMargin : CGFloat = 5
        
//        //列数
//        let col = count == 4 ? 2 :  (count >= 3 ? 3 : count)
//        //行数
//        let row = (count - 1) / 3 + 1
//        
//        let width = CGFloat(col) * itemH + CGFloat(col - 1) * itemMargin
//        let height = CGFloat(row) * itemH + CGFloat(row - 1) * itemMargin
//        
        let col = count == 4 ? 2 : (count >= 3 ? 3 : count)
        // 如果是2张 (2 - 1)/3 + 1
        // 如果是5张 (5 - 1)/3 + 1
        let row = count == 4 ? 2 : ((count - 1) / 3 + 1)
        
        // 宽度 = 列数*每一个条目的宽度 + (列数减1)*条目之前的间距
        let width = CGFloat(col) * itemWH + CGFloat(col - 1) * itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1) * itemMargin
        return CGSize(width: width, height: height)
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
        
        registerClass(HJStatusPictureViewCell.self, forCellWithReuseIdentifier: HJStatusPictureViewItemId)
        dataSource = self
        
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin  
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
         
        
        addSubview(countlabel)
        
        countlabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        reloadData()
    }
    
    // MARK: - 懒加载控件
    private lazy var countlabel :UILabel = UILabel(textColor: UIColor.blackColor(), fontSize: 40)

}

extension HJStatusPictureView :UICollectionViewDataSource{
    
    // 返回有多少个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    
    // 返回cell
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HJStatusPictureViewItemId, forIndexPath: indexPath) as! HJStatusPictureViewCell
        cell.pictureInfo = pic_urls![indexPath.row]
        cell.backgroundColor = self.backgroundColor
        return cell
    }


}

class HJStatusPictureViewCell : UICollectionViewCell {
    
    var pictureInfo: HJStatusPictureInfo?{
        
        didSet{
            
            if let urlString = pictureInfo?.thumbnail_pic {
                
                gifImageView.hidden = !urlString.hasSuffix(".gif")
                imgView.sd_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            }
        }
    }
    
    override  init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addSubview(imgView)
        addSubview(gifImageView)
        
        imgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imgView)
            make.bottom.equalTo(imgView)
        }
    }
    
    // MARK: - 懒加载
    private lazy var imgView: UIImageView = {
        
        let imageView = UIImageView()
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
}
